package com.wande.dataplatform.filecollection.controller;

import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.filecollection.domain.bo.CollectionTaskBo;
import com.wande.dataplatform.filecollection.domain.vo.CollectionTaskVo;
import com.wande.dataplatform.filecollection.domain.vo.FileRecordVo;
import com.wande.dataplatform.filecollection.service.IFileCollectionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 采集任务控制器
 *
 * @author wande
 */
@RestController
@RequestMapping("/dataplatform/file-collection/task")
@RequiredArgsConstructor
public class CollectionTaskController extends BaseController {

    private final IFileCollectionService fileCollectionService;

    /**
     * 查询采集任务列表
     *
     * @param bo 查询条件
     * @param pageQuery 分页参数
     * @return 任务列表
     */
    @GetMapping("/list")
    public TableDataInfo<CollectionTaskVo> listTasks(CollectionTaskBo bo, PageQuery pageQuery) {
        return fileCollectionService.listTasks(bo, pageQuery);
    }

    /**
     * 查询采集任务详情
     *
     * @param id 任务ID
     * @return 任务详情
     */
    @GetMapping("/{id}")
    public R<CollectionTaskVo> getTask(@PathVariable Long id) {
        return R.ok(fileCollectionService.getTask(id));
    }

    /**
     * 手动触发采集任务
     *
     * @param ruleId 规则ID
     * @return 任务ID
     */
    @Log(title = "采集任务", businessType = BusinessType.OTHER)
    @PostMapping("/trigger/{ruleId}")
    public R<Long> triggerTask(@PathVariable Long ruleId) {
        return R.ok(fileCollectionService.triggerTask(ruleId));
    }

    /**
     * 停止采集任务
     *
     * @param taskId 任务ID
     * @return 操作结果
     */
    @Log(title = "采集任务", businessType = BusinessType.OTHER)
    @PutMapping("/{taskId}/stop")
    public R<Void> stopTask(@PathVariable Long taskId) {
        fileCollectionService.stopTask(taskId);
        return R.ok();
    }

    /**
     * 重跑采集任务
     *
     * @param taskId 任务ID
     * @return 新任务ID
     */
    @Log(title = "采集任务", businessType = BusinessType.OTHER)
    @PostMapping("/{taskId}/retry")
    public R<Long> retryTask(@PathVariable Long taskId) {
        return R.ok(fileCollectionService.retryTask(taskId));
    }

    /**
     * 查询文件记录列表
     *
     * @param taskId 任务ID
     * @param pageQuery 分页参数
     * @return 文件记录列表
     */
    @GetMapping("/{taskId}/files")
    public TableDataInfo<FileRecordVo> listFileRecords(@PathVariable Long taskId, PageQuery pageQuery) {
        CollectionTaskBo bo = new CollectionTaskBo();
        bo.setId(taskId);
        return fileCollectionService.listFileRecords(bo, pageQuery);
    }

    /**
     * 查询任务统计信息
     *
     * @param ruleId 规则ID（可选）
     * @return 统计信息
     */
    @GetMapping("/statistics")
    public R<java.util.Map<String, Object>> getTaskStatistics(@RequestParam(required = false) Long ruleId) {
        return R.ok(fileCollectionService.getTaskStatistics(ruleId));
    }

    /**
     * 查询任务执行趋势
     *
     * @param ruleId 规则ID（可选）
     * @param days 天数
     * @return 趋势数据
     */
    @GetMapping("/trend")
    public R<java.util.List<java.util.Map<String, Object>>> getTaskTrend(
        @RequestParam(required = false) Long ruleId,
        @RequestParam(defaultValue = "7") Integer days) {
        return R.ok(fileCollectionService.getTaskTrend(ruleId, days));
    }
}
